/**
 *
 *
 *
 *
 *
 */

package com.jt.modules.app.controller;


import com.jt.common.utils.R;
import com.jt.common.validator.ValidatorUtils;
import com.jt.modules.app.form.LoginForm;
import com.jt.modules.app.service.UserService;
import com.jt.modules.app.utils.JwtUtils;
import com.jt.modules.sys.entity.SysUserEntity;
import com.jt.modules.sys.form.SysLoginForm;
import com.jt.modules.sys.service.SysUserService;
import com.jt.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 *
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserTokenService sysUserTokenService;

    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestBody LoginForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        String userId = userService.login(form);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

    /**
     * 点餐平板登录
     * 登录时需要传安卓设备号，这样才能确定某个服务员登录了哪台设备
     * 需要输入用户id/手机号、设备id及密码
     */
    @ApiOperation("门店平板登录")
    @PostMapping("/paduserlogin")
    public R padUserLogin(@RequestBody LoginForm form)throws IOException {

        SysUserEntity user = null;
        //平板端可以使用系统给门店分配的用户名或者手机号登录系统
        if(StringUtils.isNotBlank(form.getUserName())){
            //用户信息
            user = sysUserService.queryByUserName(form.getUserName());
        }else{
            //用户信息
            user = sysUserService.queryByUserMobile(form.getMobile());
        }

        //账号不存在、密码错误
        if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }

        //账号锁定
        if(user.getStatus() == 0){
            return R.error("账号已被锁定,请联系管理员");
        }

        String userId = user.getUserId();
        //生成token
        String token = jwtUtils.generateToken(userId,form.getDeviceId());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

}
